package edu.scause.gs.service.implement;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionContext;

import edu.scause.gs.dao.AdminDAO;
import edu.scause.gs.dao.GoodsInfoDAO;
import edu.scause.gs.dao.OrdersDAO;
import edu.scause.gs.model.Admin;
import edu.scause.gs.model.GoodsInfo;
import edu.scause.gs.model.Orders;
import edu.scause.gs.service.AdminService;
import edu.scause.gs.tools.Tools;

public class AdminService_imp implements AdminService {

	@Override
	public boolean AdminsLogin(String adminid, String adminpassword) {
		// TODO Auto-generated method stub
		AdminDAO adminDAO = new AdminDAO();
		Admin admin = adminDAO.findById(Integer.parseInt(adminid));
		if (admin != null && admin.getPassword().equals(adminpassword)) {
			ActionContext actionContext = ActionContext.getContext();
			actionContext.getSession().put("adminname", admin.getName());
			return true;
		} else {
			return false;
		}
		
	}

	@Override
	public boolean AddGoodsInfo(GoodsInfo goodsInfo) {
		// TODO Auto-generated method stub
		GoodsInfoDAO goodsInfoDAO = new GoodsInfoDAO();
		goodsInfo.setTotalSales(Long.parseLong("0"));
		try {
			Transaction ts = goodsInfoDAO.getSession().beginTransaction();
			goodsInfoDAO.save(goodsInfo);
			ts.commit();
			goodsInfoDAO.getSession().close();
			
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean ModifyGoodsInfo(GoodsInfo goodsInfo) {
		// TODO Auto-generated method stub
		GoodsInfoDAO goodsInfoDAO = new GoodsInfoDAO();
		
		try {
			Transaction ts = goodsInfoDAO.getSession().beginTransaction();
			goodsInfoDAO.merge(goodsInfo);
			ts.commit();
			goodsInfoDAO.getSession().close();
			
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<GoodsInfo> DisplayGoods() {
		// TODO Auto-generated method stub
		GoodsInfoDAO goodsInfoDAO = new GoodsInfoDAO();
		goodsInfoDAO.getSession().clear();
		return goodsInfoDAO.findAll();
	}

	@Override
	public GoodsInfo FindDetail(String goodID) {
		// TODO Auto-generated method stub
		GoodsInfoDAO goodsInfoDAO = new GoodsInfoDAO();
		GoodsInfo goodsInfo = goodsInfoDAO.findById(Integer.parseInt(goodID));
		return goodsInfo;
	}

	@Override
	public boolean DeleteGoodsInfo(String goodID) {
		// TODO Auto-generated method stub
		GoodsInfoDAO goodsInfoDAO = new GoodsInfoDAO();
		GoodsInfo goodsInfo = goodsInfoDAO.findById(Integer.parseInt(goodID));
		
		try {
			Transaction ts = goodsInfoDAO.getSession().beginTransaction();
			goodsInfoDAO.delete(goodsInfo);
			ts.commit();
			goodsInfoDAO.getSession().close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<Orders> ManageClientOrders() throws ParseException {
		// TODO Auto-generated method stub
		Tools tools = new Tools();
		OrdersDAO ordersDAO = new OrdersDAO();
		Iterator<Orders> it = ordersDAO.findAll().iterator();
		List<Orders> confirmList = new ArrayList<Orders>();
		while (it.hasNext()) {
			Orders orders = it.next();
			if (orders.getFinished() == false && (tools.get_Date(orders.getShoppingDate()) > 2)) {
				confirmList.add(orders);
			}
		}
		return confirmList;
	}

	@Override
	public boolean TerminateClientsOrder(int orderid, String adminid) {
		// TODO Auto-generated method stub
		OrdersDAO ordersDAO = new OrdersDAO();
		Orders tempOrders = ordersDAO.findById(orderid);
		Tools tools = new Tools();
		
		try {
			tools.sendMSG(adminid, tempOrders);
			Transaction ts = ordersDAO.getSession().beginTransaction();
			ordersDAO.delete(tempOrders);
			ts.commit();
			ordersDAO.getSession().close();
			
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

}
